/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataModel;
import DataModel.Country;
import java.util.Date;
import java.util.HashMap;

/**
 *
 * @author Carlos
 */
public class Project extends ProjectSummary
{

    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Date _creationDate;
    private String _bankAccount;
    private double _actualBudget;
    private PersonSummary _projectOfficial;
    private String _projectDescription;
    private String _projectObjectives;
    private Country _country;
    private OrganizationSummary _organization;
    private HashMap<Integer, Person> _contacts;
    private HashMap<Integer, OrganizationRelation> _participatingOrganizations;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructor">
    public Project(int _idProyecto, String _name, Date _creationDate, Date _initialDate,
                    Date _endingDate, String _bankAccount, double _initialBudget,
                    double _actualBudget, Country _country, OrganizationSummary organization,
                    String _projectDescription, String _projectObjectives, Enums.ProjectState _state,
                    Enums.ProjectType _type, Person[] _contacts, PersonSummary risponsible,
                    OrganizationRelation[] _involvedOrganizations)
    {
        super(_idProyecto, _name, _initialDate, _endingDate, _initialBudget, _country.getName(), organization.getName(), _state, _type);
        this._projectOfficial = risponsible;
        this._organization = organization;
        this._country = _country;
        this._creationDate = _creationDate;
        this._bankAccount = _bankAccount;
        this._actualBudget = _actualBudget;
        this._projectDescription = _projectDescription;
        this._projectObjectives = _projectObjectives;
        this._contacts = new HashMap<Integer, Person>();
        for(int contactIndex = 0; contactIndex < _contacts.length; contactIndex++)
        {
            Person currentContact = _contacts[contactIndex];
            this._contacts.put(currentContact.getId(), currentContact);
        }
        this._participatingOrganizations = new HashMap<Integer, OrganizationRelation>();
        for(int orgIndex = 0; orgIndex < _involvedOrganizations.length; orgIndex++)
        {
            OrganizationRelation currentRelation = _involvedOrganizations[orgIndex];
            this._participatingOrganizations.put(currentRelation.getOrganization().getId(), currentRelation);
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Properties">
    public Country getCountry() {
        return _country;
    }

    public OrganizationSummary getOrganization() {
        return _organization;
    }

    public double getActualBudget() {
        return _actualBudget;
    }

    public String getBankAccount() {
        return _bankAccount;
    }

    public Person[] getContacts() {
        return (Person[])_contacts.values().toArray();
    }

    public Date getCreationDate() {
        return _creationDate;
    }

    public String getProjectDescription() {
        return _projectDescription;
    }

    public String getProjectObjectives() {
        return _projectObjectives;
    }

    public PersonSummary getProjectOfficial() {
        return _projectOfficial;
    }

    public OrganizationRelation[] getParticipatingOrganizations() {
        return  _participatingOrganizations.values().toArray(new OrganizationRelation[_participatingOrganizations.size()]);
    }

    public void setActualBudget(double _actualBudget) {
        this._actualBudget = _actualBudget;
    }

    public void setBankAccount(String _bankAccount) {
        this._bankAccount = _bankAccount;
    }

    public void setProjectDescription(String _projectDescription) {
        this._projectDescription = _projectDescription;
    }

    public void setProjectObjectives(String _projectObjectives) {
        this._projectObjectives = _projectObjectives;
    }

    public void setProjectOfficial(User _projectOfficial) {
        this._projectOfficial = _projectOfficial;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    public void AddContact(Person newContact)
    {
        if(!_contacts.containsKey(newContact.getId()))
            _contacts.put(newContact.getId(), newContact);
    }

    public void RemoveContact(Person oldContact)
    {
        if(_contacts.containsKey(oldContact.getId()))
            _contacts.remove(oldContact.getId());
    }

    public void AddResponsible(User newUser, Organization org)
    {
        if(!_participatingOrganizations.containsKey(org.getId()))
            _participatingOrganizations.put(org.getId(), new OrganizationRelation(org, new User[0]));
        OrganizationRelation orgRel = _participatingOrganizations.get(org.getId());
        orgRel.AddResponsible(newUser);
    }

    public void RemoveResponsible(User oldResponsible, Organization org)
    {
        if(_participatingOrganizations.containsKey(org.getId()))
        {
            OrganizationRelation orgRel = _participatingOrganizations.get(org.getId());
            orgRel.RemoveResponsible(oldResponsible);
            if(orgRel.getProjectResponsibles().length == 0)
                _participatingOrganizations.remove(org.getId());
        }
    }
    // </editor-fold>

}
